package com.huifaapp.mall.base.dialogs;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseDialogFragment extends DialogFragment {
    public static final String BASIC_DIALOG_TAG = "basicDialogTag";
    Unbinder unbinder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, getstyle());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        View view = inflater.inflate(getDialogLayoutId(), container);
        initView(view);
        handleWindow(getDialog().getWindow());
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void showDialog(FragmentManager fm) {
        /**
         * 为了不重复显示dialog，在显示对话框之前移除正在显示的对话框。
         */
        FragmentTransaction ft = fm.beginTransaction();
        Fragment fragment = fm.findFragmentByTag(BASIC_DIALOG_TAG);
        if (null != fragment) {
            ft.remove(fragment);
        }
        this.show(ft, BASIC_DIALOG_TAG);
    }


    public abstract int getstyle();

    public abstract int getDialogLayoutId();

    public abstract void initData(Bundle savedInstanceState);

    public abstract void initView(View view);

    public abstract void handleWindow(Window window);

}
